<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productId">
              <a-input v-model="model.productId" placeholder="请输入产品编码"  :disabled="true"></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="产品架上存在时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="period">-->
<!--              <a-input-number v-model="model.period" placeholder="请输入产品架上存在时间" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="风险类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="riskType">
              <j-dict-select-tag type="list"  dictCode="risk_rank" placeholder="请选择风险类型" :disabled="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="起售金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startLine">
              <a-input-number  placeholder="请输入起售金额" style="width: 100%" :disabled="true" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单笔最低交易额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endLine">
              <a-input-number v-model="model.endLine" placeholder="请输入单笔最低交易额" :disabled="true" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="输入购买的单笔手数" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input-number v-model="model.buyMoney" placeholder="请输入购买手数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="起售时期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startDay">-->
<!--              <j-date placeholder="请选择起售时期" v-model="model.startDay"  style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="下架日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDay">-->
<!--              <j-date placeholder="请选择下架日期" v-model="model.endDay"  style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="利润率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="profitPercent">-->
<!--              <a-input-number v-model="model.profitPercent" placeholder="请输入利润率" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'BuyProductForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{

         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           productId: [
              { required: true, message: '请输入产品编码!'},
           ],
        },
        url: {
          add: "/product/product/add",
          edit: "/product/product/edit",
          queryById: "/product/product/queryById",
          balanceConfirm:"/product/product/balanceConfirm"//此处增加购买的后端url
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({buyMoney: 0},record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.balanceConfirm;//尝试更改此处
               method = 'put';
            }
            console.log(this.model);
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                //that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>